package com.happy.ribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: lifeng
 * @Date: 2018/12/6 13:53
 * @Description:测试
 */
@RestController
public class RibboService {

  @Autowired
  RestTemplate restTemplate;

  @Autowired
  LoadBalancerClient loadBalancerClient;

  @GetMapping("hello")
  @HystrixCommand(fallbackMethod = "portFallback")
  public String getPort(){
    loadBalancerClient.choose("lf-cloud-demo");//随机访问策略
    return restTemplate.getForObject("http://LF-CLOUD-DEMO/port",String.class);
  }

  public String portFallback (){
    return "sorry ribbon, it's error!";
  }
}
